;(function () {
  Array.prototype.distinct = function(){
   var arr = this,
    result = [],
    i,
    j,
    len = arr.length;
   for(i = 0; i < len; i++){
    for(j = i + 1; j < len; j++){
     if(arr[i].projectId === arr[j].projectId){
      j = ++i;
     }
    }
    result.push(arr[i]);
   }
   return result;
  }

  new Vue({
    el: '#resourceListIndex',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      data: [],
			//远洋
			SINOOCEAN: localStorage.getItem('userMes'),
			isShowDialog: false,
			projectList: [],
			selectProData: {name: '', code: ''},
			massifList: [],
			selectmassifData: {name: '', code: ''},
			buildingList: [],
			selectBuilding: {name: '', code: ''},
			synchroHouseList: [],
			selectSynchroHouseList: [],
			checkAllFlag: false,
			loadingGif: false,
			memo: '',
			ops: {
			  bar: {
			    background: '#c1c1c1'
			  }
			},
    },
    filters: {
      formatNum: function (num) {
        if(num) return hlNumberUtil.formatNum(num)
        return 0 ;
      }
    },
    methods: {
      openWindows()  {
        // if(window.open){
        //   window.open('https://test.inzhiyu.com/eamqa/page-mobile/#/home?accesskey=web_5028d0b451844b84ba2a2a2b3b9db0b5')
        // }

        // dd.biz.util.openLink({
        //     url:"https://test.inzhiyu.com/eamqa/page-mobile/#/home?accesskey=web_5028d0b451844b84ba2a2a2b3b9db0b5",//要打开链接的地址
        //     onSuccess : function(result) {
        //         alert(result)
        //     },
        //     onFail : function(err) {
        //         alert(err)
        //     }
        // })

      },
			isDisplayDialog () {
				this.isShowDialog = !this.isShowDialog;
				if (!this.isShowDialog) {
					this.resethouse()
					this.loadingGif = false;
					this.memo = '';
				}
			},
			getProject () {
				hlhttp.get({
					url: 'corpapi/sinoocean/project_resource_node_hierarchy',
					data: {pcode: '#', type: 'PROJ', hierarchy_flag: 'N'},
					success: (data) => {
						this.projectList = hlArrayUtil.replaceData(data, [{key: 'nodeName', rKey: 'name'}, {key: 'nodeCode', rKey: 'code'}]);
					}
				})
			},
			getMassif () {
				hlhttp.get({
					url: 'corpapi/sinoocean/project_resource_node_hierarchy',
					data: {pcode: this.selectProData.code ? this.selectProData.code : '#', type: 'STGAR', hierarchy_flag: 'Y'},
					success: (data) => {
						this.massifList = hlArrayUtil.replaceData(data, [{key: 'nodeName', rKey: 'name'}, {key: 'nodeCode', rKey: 'code'}]);
					}
				})
			},
			getBuilding (hierarchyFlag = 'N') {
				hlhttp.get({
					url: 'corpapi/sinoocean/project_resource_node_hierarchy',
					data: {pcode: this.selectmassifData.code ? this.selectmassifData.code : '#', type: 'LGCBD', hierarchy_flag: hierarchyFlag},
					success: (data) => {
						this.buildingList = hlArrayUtil.replaceData(data, [{key: 'nodeName', rKey: 'name'}, {key: 'nodeCode', rKey: 'code'}]);
					}
				})
			},
			//选择项目
			selectPROJ (arr) {
				console.log(arr)
				this.selectProData = {name: arr['name'], code: arr['code']}
				this.selectmassifData = {name: '', code: ''}
				this.selectBuilding = {name: '', code: ''}
				this.massifList = []
				this.getMassif()
			},
			//选择地块
			selectMassif (arr) {
				this.selectmassifData = {name: arr['name'], code: arr['code']}
				this.selectBuilding = {name: '', code: ''}
				this.getBuilding('Y')
			},
			//选择楼栋
			selectBuild (arr) {
				this.selectBuilding = {name: arr['name'], code: arr['code']}
			},
			//获取远洋详细列表
			project_resource_base_list () {
				hlhttp.get({
					url: 'corpapi/sinoocean/project_resource_base_list',
					data: {
						project_code: this.selectProData.code ? this.selectProData.code : '#',
						stage_area_code: this.selectmassifData.code ? this.selectmassifData.code : '#',
						logic_building_code: this.selectBuilding.code ? this.selectBuilding.code : '#'
					},
					success: (data) => {
						data.map(item => {
							item.flag = false
						})
						this.synchroHouseList = data;
						this.loadingGif = false;
					}
				})
			},
			//全选
			checkallhouse () {
				this.checkAllFlag = !this.checkAllFlag;
				if ( this.checkAllFlag ) {
					this.synchroHouseList.map(item => {item.flag = true})
					this.selectSynchroHouseList = JSON.parse(JSON.stringify(this.synchroHouseList))
				}
				if ( !this.checkAllFlag ) {
					this.synchroHouseList.map(item => {item.flag = false})
					this.selectSynchroHouseList = []
				}
			},
			//单选
			checkonehouse (i) {
				this.checkAllFlag = false;
				this.synchroHouseList[i].flag = !this.synchroHouseList[i].flag
				this.itemFlagTure()
			},
			// push flag = true
			itemFlagTure () {
				this.selectSynchroHouseList = []
				this.synchroHouseList.map(item => {
					if (item.flag) {
						this.selectSynchroHouseList.push(item)
					}
				})
			},
			//取消错选的房间
			delhouse (i) {
				this.checkAllFlag = false;
				this.synchroHouseList.map(item => {
					if (item.houseCode == this.selectSynchroHouseList[i].houseCode) {
						item.flag = false
					}
				})
				this.itemFlagTure()
			},
			//查询
			queryhouse () {
				if (this.selectProData.code && this.selectmassifData.code && !this.selectBuilding.code) {
					hl.toast('请选择楼栋')
					return
				}
				if (!this.selectProData.code && !this.selectmassifData.code && !this.selectBuilding.code) {
					hl.toast('请选择楼栋')
					return
				}
				this.loadingGif = true;
				this.project_resource_base_list()
			},
			//重置
			resethouse () {
				this.checkAllFlag = false;
				this.selectProData = {name: '', code: ''};
				this.selectmassifData = {name: '', code: ''};
				this.selectBuilding = {name: '', code: ''};
				this.massifList = [];
				this.buildingList = [];
				this.selectSynchroHouseList = [];
				this.synchroHouseList = [];
				this.loadingGif = false;
				this.getBuilding()
			},
			//提交审核
			postZy () {
				let data = []
				this.selectSynchroHouseList.map(item => {
					data.push({house_code: item.houseCode, project_code: item.projectCode})
				})
				if (!data.length) {
					hl.toast('请选择租赁资源')
					return
				}
				if (!this.memo.length) {
					hl.toast('请填写审批事项')
					return
				}
				hlhttp.post({
					url: 'project/resource/submit_audit',
					data: {
						"resources": JSON.stringify(data),
						"memo": this.memo ? this.memo : '#'
					},
					success: (data) => {
						hl.toast('提交审核成功')
						this.isDisplayDialog()
					}
				})
			},


      getData: function () {
        var _this = this;
        hlhttp.get({
            url: 'user/projects',
            success: function (res) {
              _this.data = res.distinct()
              loginPrem.loginList(function () {
                if ($('.rs-edit-fl').hasClass('js-access-control')) {
                    $('.rs-edit-fl').parent().html('<span style="line-height:39px;">--</span>')
                }
              })
            }
        });
      },
      //删除项目
      removeDel: function (id) {
        var _this = this;
        hllayer.confirm('你确认要删除该项目吗?',
          function () {
            hlhttp.delete({
              url: 'project?project_id=' + id,
              success: function (data) {
                hl.toast('删除成功!');
                _this.getData()
              }
            });
          }
        )
      },
      //编辑跳转页面
      editZh: function (id, name, num) {
        window.localStorage.removeItem('templateID');
        window.localStorage.removeItem('expenditureIDS');
        sessionStorage.removeItem('NAVPOSTION');

        // 获取当前主界面右侧高亮的索引值  业务工作台或者配置
        var highLightIndex = 0
        $('.tabNav-home b').each(function(index,el){
          if($(el).hasClass('color-primary')){
            highLightIndex = index
          }
        })

        hlUrlUtil.goTo('projectConfigIndex',{'ddtab':true,'highLightIndex':highLightIndex,'projectId':id},true)
      },
      //清除cookie
      removeCookie: function () {
        window.localStorage.removeItem('BUILDINGID');
        window.sessionStorage.removeItem('NAVPOSTION');
        window.localStorage.removeItem('templateID');
        window.localStorage.removeItem('expenditureIDS');
        hlUrlUtil.goTo('projectConfigIndex',{ddtab:true}, true)
      }
    },
    mounted: function () {
      this.getData()
			if (this.SINOOCEAN.indexOf('SINOOCEAN') > -1) {
				this.getProject()
				this.getBuilding()
			}
    }
  })
}())
